<?php
/**
 * @purpose
 * @Author: Madou
 * @Time  : 2022/8/6 14:19
 */

namespace App\Models\Task\SnapTasks\Repositories;

use App\Models\Train\TrainSeat\TrainSeat;
use App\Models\Train\TrainTripInfo\TrainTripInfo;
use Carbon\Carbon;

class SnapTaskTrip
{
    private string        $train_date;
    private TrainTripInfo $train_trip_info;
    private TrainSeat     $train_seat;


    public function __construct(string $train_date, TrainTripInfo $trainTripInfo, TrainSeat $trainSeat)
    {
        $this->train_date      = $train_date;
        $this->train_trip_info = $trainTripInfo;
        $this->train_seat      = $trainSeat;

    }

    // 任务键值 -用于 redis 上分
    public function getTaskTripIndex(): string
    {
        return "{$this->getTrainCode()}:{$this->getFromStationCode()}:{$this->getToStationCode()}:{$this->getTrainDate()}:{$this->getSeatName()}";
    }

    public function getTrainCode(): string
    {
        return $this->train_trip_info->getTrainCode();
    }

    public function getFromStationCode(): string
    {
        return $this->train_trip_info->getFromStationCode();
    }

    public function getToStationCode(): string
    {
        return $this->train_trip_info->getToStationCode();
    }

    public function getTrainDate(): string
    {
//		return Carbon::parse($this->train_date)->toDateString();
        return $this->train_date;
    }

    public function getSeatName(): string
    {
        return $this->train_seat->getSeatName();
    }

    public function toArray(): array
    {
        return [
            'from_station_name' => $this->getFromStationName(),
            'to_station_name'   => $this->getToStationName(),
            'depart_at'         => $this->getDepartAt(),
            'train_code'        => $this->getTrainCode(),
            'seat_name'         => $this->getSeatName(),
            'seat_price'        => $this->getSeatPrice(),
        ];
    }

    public function getFromStationName(): string
    {
        return $this->train_trip_info->getFromStation()->getStationName();
    }

    public function getToStationName(): string
    {
        return $this->train_trip_info->getToStation()->getStationName();
    }

    public function getDepartAt(): string
    {
        return Carbon::parse("{$this->train_date} {$this->train_trip_info->getDepartTime()}")->format("Y-m-d H:i");
    }

    public function getSeatPrice(): float
    {
        return $this->train_seat->getSeatPrice();
    }
}